from django.shortcuts import render, HttpResponse, redirect
from app01 import models
from app01.utils.bootstrap import BootStrapForm, BootStrapModelForm


def city_list(request):
    queryset = models.City.objects.all()
    return render(request, 'city_list.html', {'queryset':queryset})

class UpmodelForm(BootStrapModelForm):
    bootstrap_exclude_fielids = ['img']
    class Meta:
        model = models.City
        fields = "__all__"


def city_add(request):
    title = "新建城市"
    if request.method == "GET":
        form = UpmodelForm()
        return render(request,'upload_form.html', {"form":form, "title": title})

    form = UpmodelForm(data=request.POST,files=request.FILES)
    if form.is_valid():
        # 对于文件，自动保存
        # 字段+上传的路径写入到数据库
        form.save()
        return redirect("/city/list/")
    return render(request,'upload_form.html', {"form":form, "title": title})